﻿package cn.edu.zjusc.Webservice;
import java.util.List;

import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import javax.jws.*;
import javax.jws.soap.SOAPBinding;
import javax.persistence.*;

import java.math.*;


import org.jboss.wsf.spi.annotation.WebContext;


import cn.edu.zjusc.Webservice.ConfigService;
import cn.edu.zjusc.POJO.*;
import cn.edu.zjusc.Security.ProfileInterceptor;
import cn.edu.zjusc.Security.UserCave;
@Stateless
@WebService( name = "StudentExtendInfoService", serviceName = "StudentExtendInfoService")   
@SOAPBinding(style = SOAPBinding.Style.RPC)
@WebContext (contextRoot="/ZJUSCIX/Service", urlPattern="/StudentExtendInfoService")
public class StudentExtendInfoService {

	//自主考试查询
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Interceptors(ProfileInterceptor.class)
	public List<StudentExtendInfo> FilterStudentExtendInfoType1(@WebParam(name = "uc") UserCave uc,
			@WebParam(name="stuext_englishScore")double stuext_englishScore,
			@WebParam(name="stuext_majorScore")double stuext_majorScore,
			@WebParam(name="totalscore")double totalscore,
			@WebParam(name="stuext_englishResult")double stuext_englishResult,
			@WebParam(name="stuext_majorResult")double stuext_majorResult,
			@WebParam(name="stuext_practiseResult")double stuext_practiseResult,
			@WebParam(name="stuext_compreResult")double stuext_compreResult,
			@WebParam(name="stuext_sumscore")double stuext_sumscore
			){
		String bs1="",bs2="",bs3="",ms1="",ms2="",ms3="",ms4="",ms5="";
		
		if(stuext_englishScore!=0)
			bs1=" and sei.stuext_englishScore>="+stuext_englishScore;
		if(stuext_majorScore!=0)
			bs2=" and sei.stuext_majorScore>="+stuext_majorScore;
		if(totalscore!=0)
			bs3=" and "+"sei.stuext_englishScore+sei.stuext_majorScore>="+totalscore;
		if(stuext_englishResult!=0)
			ms1=" and sei.stuext_englishResult>="+stuext_englishResult;
		if(stuext_majorResult!=0)
			ms2=" and sei.stuext_majorResult>="+stuext_majorResult;
		if(stuext_practiseResult!=0)
			ms3=" and sei.stuext_practiseResult>="+stuext_practiseResult;
		if(stuext_compreResult!=0)
			ms4=" and sei.stuext_compreResult>="+stuext_compreResult;
		if(stuext_sumscore!=0)
			ms5=" and stuext_sumscore>="+stuext_sumscore;
		
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		
		
		//sei.stuext_isEnrolled='0' 
		Query query=em.createQuery("select sei from StudentExtendInfo sei where sei.stuext_isEnrolled='0'"+bs1+bs2+bs3+ms1+ms2+ms3+ms4+ms5);
		List stuExtenList=query.getResultList();
		
		em.close();
		emf.close();
		return stuExtenList;
	}
	//推荐免试查询
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Interceptors(ProfileInterceptor.class)
	public List<StudentExtendInfo> FilterStudentExtendInfoType2(@WebParam(name = "uc") UserCave uc,
			@WebParam(name="stuext_complexScore")double stuext_complexScore,
			@WebParam(name="stuext_englishResult")double stuext_englishResult,
			@WebParam(name="stuext_majorResult")double stuext_majorResult,
			@WebParam(name="stuext_practiseResult")double stuext_practiseResult,
			@WebParam(name="stuext_compreResult")double stuext_compreResult,
			@WebParam(name="stuext_sumscore")double stuext_sumscore
			){
		String bs="",ms1="",ms2="",ms3="",ms4="",ms5="";
		if(stuext_complexScore!=0)
			bs=" and sei.stuext_complexScore>="+stuext_complexScore;
		if(stuext_englishResult!=0)
			ms1=" and sei.stuext_englishResult>="+stuext_englishResult;
		if(stuext_majorResult!=0)
			ms2=" and sei.stuext_majorResult>="+stuext_majorResult;
		if(stuext_practiseResult!=0)
			ms3=" and sei.stuext_practiseResult>="+stuext_practiseResult;
		if(stuext_compreResult!=0)
			ms4=" and sei.stuext_compreResult>="+stuext_compreResult;
		if(stuext_sumscore!=0)
			ms5=" and stuext_sumscore>="+stuext_sumscore;
		
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		
		Query query=em.createQuery("select sei from StudentExtendInfo sei where sei.stuext_isEnrolled='0' "+bs+ms1+ms2+ms3+ms4+ms5);
		List stuExtenList=query.getResultList();
		
		em.close();
		emf.close();
		return stuExtenList;
	}
	//考研调剂
	public List<StudentExtendInfo> FilterStudentExtendInfoType3(){
		return null;
	}
//	public List<StudentExtendInfo> GetStudentExtendInfoByExamId(@WebParam(name="id")String id) {
//
//		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
//		EntityManager em=emf.createEntityManager();
//
//		Query query=em.createQuery("from StudentExtendInfo e where e.stuext_examCardID ='"+id+"'");
//		List stuExtendBkList=query.getResultList();
//		
//		em.close();
//		emf.close();
//		return stuExtendBkList;
//	}
	public List<StudentExtendInfo> GetStudentExtendInfoById(@WebParam(name="id")String id) {

		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();

		Query query=em.createQuery("from StudentExtendInfo e where e.stuext_id ='"+id+"'");
		List stuExtendBkList=query.getResultList();
		
		em.close();
		emf.close();
		return stuExtendBkList;
	}
	
	@Interceptors(ProfileInterceptor.class)
	public List<StudentExtendInfo> GetStudentExtendInfoByExamInfo(@WebParam(name = "uc") UserCave uc,@WebParam(name="examPlace")String examPlace,
			@WebParam(name="studentType")String studentType,
			@WebParam(name="major")int major
			) {

		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();

		Query query=em.createQuery("select e from StudentExtendInfo e,StudentBaseInfo s where e.stuext_id=s.stubs_id and e.stuext_examType='"+studentType+"' and e.stuext_examPlace='"+
				examPlace+"' and s.stubs_majorID="+major);
		List stuExtendBkList=query.getResultList();
		
		em.close();
		emf.close();
		return stuExtendBkList;
	}

	
	public String ModifyStudentScoreInfo(@WebParam(name = "astuext_id") BigDecimal stuext_id,
			@WebParam(name = "astuext_englishScore") double stuext_englishScore,
			@WebParam(name = "astuext_majorScore") double stuext_majorScore,
			@WebParam(name = "astuext_complexScore") double stuext_complexScore,
			@WebParam(name = "astuext_englishResult") double stuext_englishResult,
			@WebParam(name = "astuext_majorResult") double stuext_majorResult,
			@WebParam(name = "astuext_practiseResult") double stuext_practiseResult,
			@WebParam(name = "astuext_compreResult") double stuext_compreResult
			
		) {
		String s1,s2,s3,s4,s5,s6,s7,s8;
		double stuext_sumscore=stuext_englishScore+stuext_majorScore+stuext_complexScore+stuext_englishResult+stuext_majorResult+stuext_practiseResult+stuext_compreResult;
		s1="e.stuext_englishScore="+stuext_englishScore;
		s2=", e.stuext_majorScore="+stuext_majorScore;
		s3=", e.stuext_complexScore="+stuext_complexScore;
		s4=", e.stuext_englishResult="+stuext_englishResult;
		s5=", e.stuext_majorResult="+stuext_majorResult;
		s6=", e.stuext_practiseResult="+stuext_practiseResult;
		s7=", e.stuext_compreResult="+stuext_compreResult;
		s8=", e.stuext_sumscore="+stuext_sumscore;
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		StudentExtendInfo stu=em.find(StudentExtendInfo.class, stuext_id);
		stu.setStuext_englishScore(stuext_englishScore);
		stu.setStuext_majorScore(stuext_majorScore);
		stu.setStuext_complexScore(stuext_complexScore);
		stu.setStuext_englishResult(stuext_englishResult);
		stu.setStuext_majorResult(stuext_majorResult);
		stu.setStuext_practiseResult(stuext_practiseResult);
		stu.setStuext_compreResult(stuext_compreResult);
		stu.setStuext_sumscore(stuext_sumscore);
		//System.out.println("update StudentExtendInfo e set "+s1+s2+s3+s4+s5+s6+s7+s8+" where e.stuext_id="+stuext_id);
		//开始事务
		em.getTransaction().begin();
		//em.createQuery("update StudentExtendInfo as e set "+s1+s2+s3+s4+s5+s6+s7+s8+" where e.stuext_id="+stuext_id);
		em.merge(stu);
		//提交事务

		em.getTransaction().commit();

		em.close();
		emf.close();
		
		return "修改考生信息成功";
	}

	@Interceptors(ProfileInterceptor.class)
	public String EnrollStudent(@WebParam(name = "uc") UserCave uc,@WebParam(name="stuext_id")BigDecimal stuext_id){
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		
		em.getTransaction().begin();
		StudentExtendInfo eiBean = em.find(StudentExtendInfo.class, stuext_id);
		eiBean.setStuext_isEnrolled("1");
		em.merge(eiBean);
		
		em.getTransaction().commit();
		
		em.close();
		emf.close();
		return "录取学生";
	}
	
	public String UnEnrollStudent(@WebParam(name="stuext_id")BigDecimal stuext_id){
		
		return "不录取学生";
	}
	//面试
	@SuppressWarnings("rawtypes")
	@Interceptors(ProfileInterceptor.class)
	public String ModifyStudentRoomInfoOfFace(@WebParam(name = "uc") UserCave uc,
			@WebParam(name = "id") String id,
			@WebParam(name="room")String room){
		String ids[]=id.split(",");
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		
		for(int x=0;x<ids.length;x++){
			Query query=em.createQuery("select count(*) from StudentExtendInfo e where e.stuext_auditionGroup='"+room+"'");
			List li=query.getResultList();
			Long i;
			i=(Long)li.get(0); 
			
			int seatNum=Integer.valueOf(Long.toString(i))+1;
			if (seatNum>30) {
				return "对不起，该考场安排总人数已经大于30，超出的考生将不会为其安排考场";
			}
			//开始事务
			em.getTransaction().begin();
			StudentExtendInfo stu=em.find(StudentExtendInfo.class, new java.math.BigDecimal(ids[x]));
			stu.setStuext_auditionGroup(Integer.parseInt(room));
			stu.setStuext_auditionOrder(seatNum);
			//提交事务
			em.getTransaction().commit();
			
			System.out.println("--------------------安排面试------------------");
//			//生成准考证
//			ConfigService cfgSer=new ConfigService();
//			ConfigInfo cfgInfo=cfgSer.readConfig();
//			String s1=Integer.valueOf(Integer.valueOf(cfgInfo.getYear()).intValue()%100).toString();//年份
//			
//			String s2="33431z";//省份
//			String s3=cfgInfo.getSeason();//季节
//			if(s3.equals("0"))
//				s3="1";
//			else if(s3.equals("1"))
//				s3="6";
//			String s4=String.format("%03d", Integer.valueOf(room));//考试地点
//			String s5=String.format("%02d", seatNum); //自编号
//			String exCard= s1+s2+s3+s4+s5;
//			stu.setStuext_examCardID(exCard);
			//开始事务
			//em.getTransaction().begin();
			//em.createQuery("update StudentExtendInfo e set e.stuext_roomNum='"+room+"' where e.stuext_id="+id);
			
			//提交事务
			//em.getTransaction().commit();
			}
			em.close();
			emf.close();
		return "安排面试成功";
	}
	
	@SuppressWarnings("rawtypes")
	@Interceptors(ProfileInterceptor.class)
	public String ModifyStudentRoomInfo(@WebParam(name = "uc") UserCave uc,
			@WebParam(name = "id") String id,
			@WebParam(name="room")int room
		) throws Exception {
		String ids[]=id.split(",");
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		for(int x=0;x<ids.length;x++){
		Query query=em.createQuery("select count(*) from StudentExtendInfo e where e.stuext_roomID='"+room+"'");
		List li=query.getResultList();
		Long i;
		i=(Long)li.get(0); 
		
		int seatNum=Integer.valueOf(Long.toString(i))+1;
		if (seatNum>30) {
			return "对不起，该考场安排总人数已经大于30，超出的考生将不会为其安排考场";
		}
		StudentExtendInfo stu=em.find(StudentExtendInfo.class, new java.math.BigDecimal(ids[x]));
		stu.setStuext_roomID(room);
		stu.setStuext_seatNumber(seatNum);
		
		//生成准考证
		ConfigService cfgSer=new ConfigService();
		ConfigInfo cfgInfo=cfgSer.readConfig();
		String s1=Integer.valueOf(Integer.valueOf(cfgInfo.getYear()).intValue()%100).toString();//年份
		
		String s2="33431z";//省份
		String s3=cfgInfo.getSeason();//季节
		if(s3.equals("0"))
			s3="1";
		else if(s3.equals("1"))
			s3="6";
		String s4=String.format("%03d", Integer.valueOf(room));//考试地点
		String s5=String.format("%02d", seatNum); //自编号
		String exCard= s1+s2+s3+s4+s5;
		stu.setStuext_examCardID(exCard);
		//开始事务
		em.getTransaction().begin();
		//em.createQuery("update StudentExtendInfo e set e.stuext_roomNum='"+room+"' where e.stuext_id="+id);
		em.merge(stu);
		//提交事务
		em.getTransaction().commit();
		}
		em.close();
		emf.close();
		
		return "安排成功";
	}
	
	/**
	 * 检查有没有用户名重复
	 * @param uname
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public String VerifyUserName(@WebParam(name="uname")String uname){
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		
		Query query=em.createQuery("from StudentExtendInfo e where e.stuext_userName ='"+uname+"'");
		List li=query.getResultList();
		em.close();
		emf.close();
		if(li.size()>0)
			return "0";
		else
			return "1";
	}

}
